
import sys, os, math, random
#import alsaaudio, audioop
import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *
from direct.showbase.DirectObject import DirectObject
from direct.actor import Actor
from direct.task import Task

random.seed()

class Sheep(Actor.Actor):
  state = 0
  def __init__(self, parent):
    Actor.Actor.__init__(self)
    self.loadModel('gorillawalking.egg')
    self.loadAnims({'gorillawalking':'gorillawalking.egg'})
    self.loop('gorillawalking')
    self.setScale(0.2,0.2,0.2)
    self.setPos(2,2,0)
    self.reparentTo(parent)
  def toball(self, parent):
    self.reparentTo(parent)
#    self.obj.lookAt(parent)
    state = 1
  def toworld(self, parent,dist):
    oldpar = self.getParent()
    self.reparentTo(parent)
    self.setPos(oldpar, 0, 3+dist, 0)
    self.setHpr(0,0,0)
    state = 0
  def move(self,x,y,z,h,p,r):
    self.setPosHpr(x,y,z,h,p,r)
 
class World(DirectObject):
  def __init__(self):
    taskMgr.add(self.SpinCameraTask, "SpinCameraTask")
#    taskMgr.add(self.checkMic, "checkMic")
    self.dude = Actor.Actor()
    self.dude.loadModel('ralph.egg')
    self.dude.loadAnims({'ralph-walk':'ralph-walk.egg'})
    self.dude.loop('ralph-walk')
    self.dude.reparentTo(render)

    self.sheeplist = []
    self.sheeponball = 0

    self.accept("z",self.addSheep)
    self.accept("x",self.pushSheep)
    self.accept("c",self.popSheep)
   
#    self.inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)
#    self.inp.setchannels(1)
#    self.inp.setrate(8000)
#    self.inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
#    self.inp.setperiodsize(320)  
  
  def SpinCameraTask(self, task):
    angledegrees = task.time * 6.0
    angleradians = angledegrees * (math.pi / 180.0)
    base.camera.setPos(20*math.sin(angleradians),-20.0*math.cos(angleradians),5)
    base.camera.setHpr(angledegrees, 0, 0)
    return Task.cont

  def addSheep(self):
    self.sheeplist.append(Sheep(render))
    
  def pushSheep(self):
    if len(self.sheeplist) > self.sheeponball:
      self.sheeponball += 1
      radius = max(math.sqrt(self.sheeponball)/2.5,1.5)
#    fradius = math.floor(radius)
#    sheepnum = (self.sheeponball - fradius**2)/((fradius+1)**2-fradius**2)
#    sheephangle = (2*(self.sheeponball%2)-1)*math.pi*sheepnum
#    sheepangle = (math.pi*10*sheepnum)%(2*math.pi)
#    print [self.sheeponball, sheepnum, sheephangle, sheepangle]
      sheephangle = random.uniform(-math.pi,math.pi)
      sheepangle = random.uniform(0,2*math.pi)
      x = (radius)*math.sin(sheephangle)*math.cos(sheepangle)
      y = (radius)*math.cos(sheephangle)
      z = (radius)*math.sin(sheephangle)*math.sin(sheepangle) + 5
      self.sheeplist[self.sheeponball-1].move(x,y,z,random.randint(0,360),random.randint(0,360),random.randint(0,360))
      self.sheeplist[self.sheeponball-1].toball(self.dude)
    
  def popSheep(self):
    if self.sheeponball:
      self.sheeponball -= 1
      self.sheeplist[self.sheeponball].toworld(render, len(self.sheeplist)-self.sheeponball)

'''  def checkMic(self, task):
    l,data = self.inp.read()
    if l:
      # clear out the old buffer first
      while l:
        olddata = data
        l,data = self.inp.read()
      # check if there is enough noise
      if audioop.rms(olddata,2) > 100:
        self.popSheep()
    return task.cont
'''
w = World()
run()

